package item;

import inventory.EquipLocation;
import effects.Effect;
import factory.EffectFactory;
import loadsave.Memento;
import loadsave.loadsaveobject.Iterator;
import loadsave.loadsaveobject.LoadSaveObject;
import loadsave.loadsaveobject.LoadSaveObjectComposite;
import loadsave.loadsaveobject.LoadSavePrimitive;

public class UsableItem extends TakeableItem {

    private Effect effect;
    
    public UsableItem( String name, Effect effect) {
        super(name);
        this.effect = effect;
    }
    
    /**
     * have the visitor visit this item to pick it up
     */
    public void accept( visitors.TakeableItemVisitor tiv ) {
        tiv.visitUsableItem(this);
    }
    
    /**
     * accessor for the effect that this item invokes when used
     * @return
     */
    public Effect getEffect() {
        return effect;
    }

    @Override
    public Memento getMemento() {
        return new Memento() {

            @Override
            public LoadSaveObject getState() {
                LoadSaveObjectComposite toReturn = new LoadSaveObjectComposite("", "Equippable");
                Iterator<LoadSaveObject> i = UsableItem.super.getMemento().getState().getIterator();
                for (i.init(); i.hasNext(); i.next()) {
                	toReturn.add(i.getCurrKey().getValue(), i.getCurrVal());
                }
               LoadSaveObject e = effect.getMemento().getState();
                e.setName("effect");
                toReturn.add(e.getName(), e);
                return toReturn;
            }

            @Override
            public void restoreState(LoadSaveObject lso) {
            	UsableItem.super.getMemento().restoreState(lso);
                effect = EffectFactory.createEffect(lso.get("effect"));
                effect.getMemento().restoreState(lso.get("effect"));
            }
            
        };
    }

    public UsableItem() { }
    
    @Override
	public UsableItem clone() {
		UsableItem toReturn = new UsableItem();
		toReturn.getMemento().restoreState(this.getMemento().getState());
		return toReturn;
	}

}
